home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume22 / gawk2.11 / part08 < prev    next >
Encoding:
Internet Message Format  |  1990-06-07  |  54.7 KB

  1. Subject:  v22i094:  GNU AWK, version 2.11, Part08/16
  2. Newsgroups: comp.sources.unix
  3. Approved: rsalz@uunet.UU.NET
  4. X-Checksum-Snefru: 214df279 5660dfb6 3cc47d05 8fc58779
  5.  
  6. Submitted-by: "Arnold D. Robbins" <arnold@unix.cc.emory.edu>
  7. Posting-number: Volume 22, Issue 94
  8. Archive-name: gawk2.11/part08
  9.  
  10. #! /bin/sh
  11. # This is a shell archive.  Remove anything before this line, then feed it
  12. # into a shell via "sh file" or similar.  To overwrite existing files,
  13. # type "sh file -c".
  14. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  15. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  16. # Contents:  ./missing.c ./missing.d/vprintf.c ./regex.c.01
  17. # Wrapped by rsalz@litchi.bbn.com on Wed Jun  6 12:24:53 1990
  18. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  19. echo If this archive is complete, you will see the following message:
  20. echo '          "shar: End of archive 8 (of 16)."'
  21. if test -f './missing.c' -a "${1}" != "-c" ; then 
  22.   echo shar: Will not clobber existing file \"'./missing.c'\"
  23. else
  24.   echo shar: Extracting \"'./missing.c'\" \(1248 characters\)
  25.   sed "s/^X//" >'./missing.c' <<'END_OF_FILE'
  26. X#ifdef MSDOS
  27. X#define BCOPY_MISSING
  28. X#define STRCASE_MISSING
  29. X#define BLKSIZE_MISSING
  30. X#define SPRINTF_INT
  31. X#define RANDOM_MISSING
  32. X#define GETOPT_MISSING
  33. X#endif
  34. X
  35. X#ifdef DUP2_MISSING
  36. X#include "missing.d/dup2.c"
  37. X#endif /* DUP2_MISSING */
  38. X
  39. X#ifdef GCVT_MISSING
  40. X#include "missing.d/gcvt.c"
  41. X#endif /* GCVT_MISSING */
  42. X
  43. X#ifdef GETOPT_MISSING
  44. X#include "missing.d/getopt.c"
  45. X#endif    /* GETOPT_MISSING */
  46. X
  47. X#ifdef MEMCMP_MISSING
  48. X#include "missing.d/memcmp.c"
  49. X#endif    /* MEMCMP_MISSING */
  50. X
  51. X#ifdef MEMCPY_MISSING
  52. X#include "missing.d/memcpy.c"
  53. X#endif    /* MEMCPY_MISSING */
  54. X
  55. X#ifdef MEMSET_MISSING
  56. X#include "missing.d/memset.c"
  57. X#endif    /* MEMSET_MISSING */
  58. X
  59. X#ifdef RANDOM_MISSING
  60. X#include "missing.d/random.c"
  61. X#endif    /* RANDOM_MISSING */
  62. X
  63. X#ifdef STRCASE_MISSING
  64. X#include "missing.d/strcase.c"
  65. X#endif    /* STRCASE_MISSING */
  66. X
  67. X#ifdef STRCHR_MISSING
  68. X#include "missing.d/strchr.c"
  69. X#endif    /* STRCHR_MISSING */
  70. X
  71. X#ifdef STRERROR_MISSING
  72. X#include "missing.d/strerror.c"
  73. X#endif    /* STRERROR_MISSING */
  74. X
  75. X#ifdef STRTOD_MISSING
  76. X#include "missing.d/strtod.c"
  77. X#endif    /* STRTOD_MISSING */
  78. X
  79. X#ifdef TMPNAM_MISSING
  80. X#include "missing.d/tmpnam.c"
  81. X#endif    /* TMPNAM_MISSING */
  82. X
  83. X#if defined(VPRINTF_MISSING) && defined(BSDSTDIO)
  84. X#include "missing.d/vprintf.c"
  85. X#endif    /* VPRINTF_MISSING && BSDSTDIO */
  86. END_OF_FILE
  87.   if test 1248 -ne `wc -c <'./missing.c'`; then
  88.     echo shar: \"'./missing.c'\" unpacked with wrong size!
  89.   fi
  90.   # end of './missing.c'
  91. fi
  92. if test -f './missing.d/vprintf.c' -a "${1}" != "-c" ; then 
  93.   echo shar: Will not clobber existing file \"'./missing.d/vprintf.c'\"
  94. else
  95.   echo shar: Extracting \"'./missing.d/vprintf.c'\" \(844 characters\)
  96.   sed "s/^X//" >'./missing.d/vprintf.c' <<'END_OF_FILE'
  97. X#include <stdio.h>
  98. X#include <varargs.h>
  99. X
  100. X#ifndef BUFSIZ
  101. X#include <stdio.h>
  102. X#endif
  103. X
  104. X#ifndef va_dcl
  105. X#include <varargs.h>
  106. X#endif
  107. X
  108. Xint
  109. Xvsprintf(str, fmt, ap)
  110. X    char *str, *fmt;
  111. X    va_list ap;
  112. X{
  113. X    FILE f;
  114. X    int len;
  115. X
  116. X    f._flag = _IOWRT+_IOSTRG;
  117. X    f._ptr = (char *)str;    /* My copy of BSD stdio.h has this as (char *)
  118. X                 * with a comment that it should be
  119. X                 * (unsigned char *).  Since this code is
  120. X                 * intended for use on a vanilla BSD system,
  121. X                 * we'll stick with (char *) for now.
  122. X                 */
  123. X    f._cnt = 32767;
  124. X    len = _doprnt(fmt, ap, &f);
  125. X    *f._ptr = 0;
  126. X    return (len);
  127. X}
  128. X
  129. Xint
  130. Xvfprintf(iop, fmt, ap)
  131. X    FILE *iop;
  132. X    char *fmt;
  133. X    va_list ap;
  134. X{
  135. X    int len;
  136. X
  137. X    len = _doprnt(fmt, ap, iop);
  138. X    return (ferror(iop) ? EOF : len);
  139. X}
  140. X
  141. Xint
  142. Xvprintf(fmt, ap)
  143. X    char *fmt;
  144. X    va_list ap;
  145. X{
  146. X    int len;
  147. X
  148. X    len = _doprnt(fmt, ap, stdout);
  149. X    return (ferror(stdout) ? EOF : len);
  150. X}
  151. END_OF_FILE
  152.   if test 844 -ne `wc -c <'./missing.d/vprintf.c'`; then
  153.     echo shar: \"'./missing.d/vprintf.c'\" unpacked with wrong size!
  154.   fi
  155.   # end of './missing.d/vprintf.c'
  156. fi
  157. if test -f './regex.c.01' -a "${1}" != "-c" ; then 
  158.   echo shar: Will not clobber existing file \"'./regex.c.01'\"
  159. else
  160.   echo shar: Extracting \"'./regex.c.01'\" \(49494 characters\)
  161.   sed "s/^X//" >'./regex.c.01' <<'END_OF_FILE'
  162. X/* Extended regular expression matching and search.
  163. X   Copyright (C) 1985 Free Software Foundation, Inc.
  164. X
  165. X               NO WARRANTY
  166. X
  167. X  BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  168. XNO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  169. XWHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  170. XRICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  171. XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  172. XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  173. XFITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  174. XAND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  175. XDEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  176. XCORRECTION.
  177. X
  178. X IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  179. XSTALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  180. XWHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  181. XLIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  182. XOTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  183. XUSE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  184. XDATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  185. XA FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  186. XPROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  187. XDAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  188. X
  189. X        GENERAL PUBLIC LICENSE TO COPY
  190. X
  191. X  1. You may copy and distribute verbatim copies of this source file
  192. Xas you receive it, in any medium, provided that you conspicuously and
  193. Xappropriately publish on each copy a valid copyright notice "Copyright
  194. X(C) 1985 Free Software Foundation, Inc."; and include following the
  195. Xcopyright notice a verbatim copy of the above disclaimer of warranty
  196. Xand of this License.  You may charge a distribution fee for the
  197. Xphysical act of transferring a copy.
  198. X
  199. X  2. You may modify your copy or copies of this source file or
  200. Xany portion of it, and copy and distribute such modifications under
  201. Xthe terms of Paragraph 1 above, provided that you also do the following:
  202. X
  203. X    a) cause the modified files to carry prominent notices stating
  204. X    that you changed the files and the date of any change; and
  205. X
  206. X    b) cause the whole of any work that you distribute or publish,
  207. X    that in whole or in part contains or is a derivative of this
  208. X    program or any part thereof, to be licensed at no charge to all
  209. X    third parties on terms identical to those contained in this
  210. X    License Agreement (except that you may choose to grant more extensive
  211. X    warranty protection to some or all third parties, at your option).
  212. X
  213. X    c) You may charge a distribution fee for the physical act of
  214. X    transferring a copy, and you may at your option offer warranty
  215. X    protection in exchange for a fee.
  216. X
  217. XMere aggregation of another unrelated program with this program (or its
  218. Xderivative) on a volume of a storage or distribution medium does not bring
  219. Xthe other program under the scope of these terms.
  220. X
  221. X  3. You may copy and distribute this program (or a portion or derivative
  222. Xof it, under Paragraph 2) in object code or executable form under the terms
  223. Xof Paragraphs 1 and 2 above provided that you also do one of the following:
  224. X
  225. X    a) accompany it with the complete corresponding machine-readable
  226. X    source code, which must be distributed under the terms of
  227. X    Paragraphs 1 and 2 above; or,
  228. X
  229. X    b) accompany it with a written offer, valid for at least three
  230. X    years, to give any third party free (except for a nominal
  231. X    shipping charge) a complete machine-readable copy of the
  232. X    corresponding source code, to be distributed under the terms of
  233. X    Paragraphs 1 and 2 above; or,
  234. X
  235. X    c) accompany it with the information you received as to where the
  236. X    corresponding source code may be obtained.  (This alternative is
  237. X    allowed only for noncommercial distribution and only if you
  238. X    received the program in object code or executable form alone.)
  239. X
  240. XFor an executable file, complete source code means all the source code for
  241. Xall modules it contains; but, as a special exception, it need not include
  242. Xsource code for modules which are standard libraries that accompany the
  243. Xoperating system on which the executable file runs.
  244. X
  245. X  4. You may not copy, sublicense, distribute or transfer this program
  246. Xexcept as expressly provided under this License Agreement.  Any attempt
  247. Xotherwise to copy, sublicense, distribute or transfer this program is void and
  248. Xyour rights to use the program under this License agreement shall be
  249. Xautomatically terminated.  However, parties who have received computer
  250. Xsoftware programs from you with this License Agreement will not have
  251. Xtheir licenses terminated so long as such parties remain in full compliance.
  252. X
  253. X  5. If you wish to incorporate parts of this program into other free
  254. Xprograms whose distribution conditions are different, write to the Free
  255. XSoftware Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  256. Xworked out a simple rule that can be stated here, but we will often permit
  257. Xthis.  We will be guided by the two goals of preserving the free status of
  258. Xall derivatives of our free software and of promoting the sharing and reuse of
  259. Xsoftware.
  260. X
  261. X
  262. XIn other words, you are welcome to use, share and improve this program.
  263. XYou are forbidden to forbid anyone else to use, share and improve
  264. Xwhat you give them.   Help stamp out software-hoarding!  */
  265. X
  266. X#ifdef MSDOS
  267. X#include <malloc.h>
  268. Xstatic  void init_syntax_once(void );
  269. Xextern  int re_set_syntax(int syntax);
  270. Xextern  char *re_compile_pattern(char *pattern,int size,struct re_pattern_buffer *bufp);
  271. Xstatic  int store_jump(char *from,char opcode,char *to);
  272. Xstatic  int insert_jump(char op,char *from,char *to,char *current_end);
  273. Xextern  void re_compile_fastmap(struct re_pattern_buffer *bufp);
  274. Xextern  int re_search(struct re_pattern_buffer *pbufp,char *string,int size,int startpos,int range,struct re_registers *regs);
  275. Xextern  int re_search_2(struct re_pattern_buffer *pbufp,char *string1,int size1,char *string2,int size2,int startpos,int range,struct re_registers *regs,int mstop);
  276. Xextern  int re_match(struct re_pattern_buffer *pbufp,char *string,int size,int pos,struct re_registers *regs);
  277. Xextern  int re_match_2(struct re_pattern_buffer *pbufp,unsigned char *string1,int size1,unsigned char *string2,int size2,int pos,struct re_registers *regs,int mstop);
  278. Xstatic  int bcmp_translate(unsigned char *s1,unsigned char *s2,int len,unsigned char *translate);
  279. Xextern  char *re_comp(char *s);
  280. Xextern  int re_exec(char *s);
  281. X#endif
  282. X
  283. X/* To test, compile with -Dtest.
  284. X This Dtestable feature turns this into a self-contained program
  285. X which reads a pattern, describes how it compiles,
  286. X then reads a string and searches for it.  */
  287. X
  288. X#ifdef emacs
  289. X
  290. X/* The `emacs' switch turns on certain special matching commands
  291. X that make sense only in emacs. */
  292. X
  293. X#include "config.h"
  294. X#include "lisp.h"
  295. X#include "buffer.h"
  296. X#include "syntax.h"
  297. X
  298. X#else  /* not emacs */
  299. X
  300. X#ifdef BCOPY_MISSING
  301. X#define bcopy(s,d,n)    memcpy((d),(s),(n))
  302. X#define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  303. X#define bzero(s,n)    memset((s),0,(n))
  304. X#else
  305. Xvoid bcopy();
  306. Xint bcmp();
  307. Xvoid bzero();
  308. X#endif
  309. X
  310. X/* Make alloca work the best possible way.  */
  311. X#ifdef __GNUC__
  312. X#define alloca __builtin_alloca
  313. X#else
  314. X#ifdef sparc
  315. X#include <alloca.h>
  316. X#endif
  317. X#endif
  318. X
  319. X/*
  320. X * Define the syntax stuff, so we can do the \<...\> things.
  321. X */
  322. X
  323. X#ifndef Sword /* must be non-zero in some of the tests below... */
  324. X#define Sword 1
  325. X#endif
  326. X
  327. X#define SYNTAX(c) re_syntax_table[c]
  328. X
  329. X#ifdef SYNTAX_TABLE
  330. X
  331. Xchar *re_syntax_table;
  332. X
  333. X#else
  334. X
  335. Xstatic char re_syntax_table[256];
  336. X
  337. Xstatic void
  338. Xinit_syntax_once ()
  339. X{
  340. X   register int c;
  341. X   static int done = 0;
  342. X
  343. X   if (done)
  344. X     return;
  345. X
  346. X   bzero (re_syntax_table, sizeof re_syntax_table);
  347. X
  348. X   for (c = 'a'; c <= 'z'; c++)
  349. X     re_syntax_table[c] = Sword;
  350. X
  351. X   for (c = 'A'; c <= 'Z'; c++)
  352. X     re_syntax_table[c] = Sword;
  353. X
  354. X   for (c = '0'; c <= '9'; c++)
  355. X     re_syntax_table[c] = Sword;
  356. X
  357. X   done = 1;
  358. X}
  359. X
  360. X#endif /* SYNTAX_TABLE */
  361. X#endif /* not emacs */
  362. X
  363. X#include "regex.h"
  364. X
  365. X/* Number of failure points to allocate space for initially,
  366. X when matching.  If this number is exceeded, more space is allocated,
  367. X so it is not a hard limit.  */
  368. X
  369. X#ifndef NFAILURES
  370. X#define NFAILURES 80
  371. X#endif /* NFAILURES */
  372. X
  373. X/* width of a byte in bits */
  374. X
  375. X#define BYTEWIDTH 8
  376. X
  377. X#ifndef SIGN_EXTEND_CHAR
  378. X#define SIGN_EXTEND_CHAR(x) (x)
  379. X#endif
  380. X
  381. Xstatic int obscure_syntax = 0;
  382. X
  383. X/* Specify the precise syntax of regexp for compilation.
  384. X   This provides for compatibility for various utilities
  385. X   which historically have different, incompatible syntaxes.
  386. X
  387. X   The argument SYNTAX is a bit-mask containing the two bits
  388. X   RE_NO_BK_PARENS and RE_NO_BK_VBAR.  */
  389. X
  390. Xint
  391. Xre_set_syntax (syntax)
  392. X{
  393. X  int ret;
  394. X
  395. X  ret = obscure_syntax;
  396. X  obscure_syntax = syntax;
  397. X  return ret;
  398. X}
  399. X
  400. X/* re_compile_pattern takes a regular-expression string
  401. X   and converts it into a buffer full of byte commands for matching.
  402. X
  403. X  PATTERN   is the address of the pattern string
  404. X  SIZE      is the length of it.
  405. X  BUFP        is a  struct re_pattern_buffer *  which points to the info
  406. X        on where to store the byte commands.
  407. X        This structure contains a  char *  which points to the
  408. X        actual space, which should have been obtained with malloc.
  409. X        re_compile_pattern may use  realloc  to grow the buffer space.
  410. X
  411. X  The number of bytes of commands can be found out by looking in
  412. X  the  struct re_pattern_buffer  that bufp pointed to,
  413. X  after re_compile_pattern returns.
  414. X*/
  415. X
  416. X#define PATPUSH(ch) (*b++ = (char) (ch))
  417. X
  418. X#define PATFETCH(c) \
  419. X {if (p == pend) goto end_of_pattern; \
  420. X  c = * (unsigned char *) p++; \
  421. X  if (translate) c = translate[c]; }
  422. X
  423. X#define PATFETCH_RAW(c) \
  424. X {if (p == pend) goto end_of_pattern; \
  425. X  c = * (unsigned char *) p++; }
  426. X
  427. X#define PATUNFETCH p--
  428. X
  429. X#ifdef MSDOS
  430. X#define MaxAllocation (1<<14)
  431. X#else
  432. X#define MaxAllocation (1<<16)
  433. X#endif
  434. X#define EXTEND_BUFFER \
  435. X  { char *old_buffer = bufp->buffer; \
  436. X    if (bufp->allocated == MaxAllocation) goto too_big; \
  437. X    bufp->allocated *= 2; \
  438. X    if (bufp->allocated > MaxAllocation) bufp->allocated = MaxAllocation; \
  439. X    if (!(bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated))) \
  440. X      goto memory_exhausted; \
  441. X    c = bufp->buffer - old_buffer; \
  442. X    b += c; \
  443. X    if (fixup_jump) \
  444. X      fixup_jump += c; \
  445. X    if (laststart) \
  446. X      laststart += c; \
  447. X    begalt += c; \
  448. X    if (pending_exact) \
  449. X      pending_exact += c; \
  450. X  }
  451. X
  452. X#ifdef NEVER
  453. X#define EXTEND_BUFFER \
  454. X  { unsigned b_off = b - bufp->buffer, \
  455. X             f_off, l_off, p_off, \
  456. X                beg_off = begalt - bufp->buffer; \
  457. X    if (fixup_jump) \
  458. X       f_off = fixup_jump - bufp->buffer; \
  459. X    if (laststart) \
  460. X       l_off = laststart - bufp->buffer; \
  461. X    if (pending_exact) \
  462. X       p_off = pending_exact - bufp->buffer; \
  463. X    if (bufp->allocated == MaxAllocation) goto too_big; \
  464. X    bufp->allocated *= 2; \
  465. X    if (bufp->allocated > MaxAllocation) bufp->allocated = MaxAllocation; \
  466. X    if (!(bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated))) \
  467. X      goto memory_exhausted; \
  468. X    b = bufp->buffer + b_off; \
  469. X    if (fixup_jump) \
  470. X      fixup_jump = bufp->buffer + f_off; \
  471. X    if (laststart) \
  472. X      laststart = bufp->buffer + l_off; \
  473. X    begalt = bufp->buffer + beg_off; \
  474. X    if (pending_exact) \
  475. X      pending_exact = bufp->buffer + p_off; \
  476. X  }
  477. X#endif
  478. Xstatic int store_jump (), insert_jump ();
  479. X
  480. Xchar *
  481. Xre_compile_pattern (pattern, size, bufp)
  482. X     char *pattern;
  483. X     int size;
  484. X     struct re_pattern_buffer *bufp;
  485. X{
  486. X  register char *b = bufp->buffer;
  487. X  register char *p = pattern;
  488. X  char *pend = pattern + size;
  489. X  register unsigned c, c1;
  490. X  char *p1;
  491. X  unsigned char *translate = (unsigned char *) bufp->translate;
  492. X
  493. X  /* address of the count-byte of the most recently inserted "exactn" command.
  494. X    This makes it possible to tell whether a new exact-match character
  495. X    can be added to that command or requires a new "exactn" command. */
  496. X     
  497. X  char *pending_exact = 0;
  498. X
  499. X  /* address of the place where a forward-jump should go
  500. X    to the end of the containing expression.
  501. X    Each alternative of an "or", except the last, ends with a forward-jump
  502. X    of this sort. */
  503. X
  504. X  char *fixup_jump = 0;
  505. X
  506. X  /* address of start of the most recently finished expression.
  507. X    This tells postfix * where to find the start of its operand. */
  508. X
  509. X  char *laststart = 0;
  510. X
  511. X  /* In processing a repeat, 1 means zero matches is allowed */
  512. X
  513. X  char zero_times_ok;
  514. X
  515. X  /* In processing a repeat, 1 means many matches is allowed */
  516. X
  517. X  char many_times_ok;
  518. X
  519. X  /* address of beginning of regexp, or inside of last \( */
  520. X
  521. X  char *begalt = b;
  522. X
  523. X  /* Stack of information saved by \( and restored by \).
  524. X     Four stack elements are pushed by each \(:
  525. X       First, the value of b.
  526. X       Second, the value of fixup_jump.
  527. X       Third, the value of regnum.
  528. X       Fourth, the value of begalt.  */
  529. X
  530. X  int stackb[40];
  531. X  int *stackp = stackb;
  532. X  int *stacke = stackb + 40;
  533. X  int *stackt;
  534. X
  535. X  /* Counts \('s as they are encountered.  Remembered for the matching \),
  536. X     where it becomes the "register number" to put in the stop_memory command */
  537. X
  538. X  int regnum = 1;
  539. X
  540. X  bufp->fastmap_accurate = 0;
  541. X
  542. X#ifndef emacs
  543. X#ifndef SYNTAX_TABLE
  544. X  /*
  545. X   * Initialize the syntax table.
  546. X   */
  547. X   init_syntax_once();
  548. X#endif
  549. X#endif
  550. X
  551. X  if (bufp->allocated == 0)
  552. X    {
  553. X      bufp->allocated = 28;
  554. X      if (bufp->buffer)
  555. X    /* EXTEND_BUFFER loses when bufp->allocated is 0 */
  556. X    bufp->buffer = (char *) realloc (bufp->buffer, 28);
  557. X      else
  558. X    /* Caller did not allocate a buffer.  Do it for him */
  559. X    bufp->buffer = (char *) malloc (28);
  560. X      if (!bufp->buffer) goto memory_exhausted;
  561. X      begalt = b = bufp->buffer;
  562. X    }
  563. X
  564. X  while (p != pend)
  565. X    {
  566. X      if (b - bufp->buffer > bufp->allocated - 10)
  567. X    /* Note that EXTEND_BUFFER clobbers c */
  568. X    EXTEND_BUFFER;
  569. X
  570. X      PATFETCH (c);
  571. X
  572. X      switch (c)
  573. X    {
  574. X    case '$':
  575. X      if (obscure_syntax & RE_TIGHT_VBAR)
  576. X        {
  577. X          if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS) && p != pend)
  578. X        goto normal_char;
  579. X          /* Make operand of last vbar end before this `$'.  */
  580. X          if (fixup_jump)
  581. X        store_jump (fixup_jump, jump, b);
  582. X          fixup_jump = 0;
  583. X          PATPUSH (endline);
  584. X          break;
  585. X        }
  586. X
  587. X      /* $ means succeed if at end of line, but only in special contexts.
  588. X        If randomly in the middle of a pattern, it is a normal character. */
  589. X      if (p == pend || *p == '\n'
  590. X          || (obscure_syntax & RE_CONTEXT_INDEP_OPS)
  591. X          || (obscure_syntax & RE_NO_BK_PARENS
  592. X          ? *p == ')'
  593. X          : *p == '\\' && p[1] == ')')
  594. X          || (obscure_syntax & RE_NO_BK_VBAR
  595. X          ? *p == '|'
  596. X          : *p == '\\' && p[1] == '|'))
  597. X        {
  598. X          PATPUSH (endline);
  599. X          break;
  600. X        }
  601. X      goto normal_char;
  602. X
  603. X    case '^':
  604. X      /* ^ means succeed if at beg of line, but only if no preceding pattern. */
  605. X
  606. X      if (laststart && p[-2] != '\n'
  607. X          && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  608. X        goto normal_char;
  609. X      if (obscure_syntax & RE_TIGHT_VBAR)
  610. X        {
  611. X          if (p != pattern + 1
  612. X          && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  613. X        goto normal_char;
  614. X          PATPUSH (begline);
  615. X          begalt = b;
  616. X        }
  617. X      else
  618. X        PATPUSH (begline);
  619. X      break;
  620. X
  621. X    case '+':
  622. X    case '?':
  623. X      if (obscure_syntax & RE_BK_PLUS_QM)
  624. X        goto normal_char;
  625. X    handle_plus:
  626. X    case '*':
  627. X      /* If there is no previous pattern, char not special. */
  628. X      if (!laststart && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  629. X        goto normal_char;
  630. X      /* If there is a sequence of repetition chars,
  631. X         collapse it down to equivalent to just one.  */
  632. X      zero_times_ok = 0;
  633. X      many_times_ok = 0;
  634. X      while (1)
  635. X        {
  636. X          zero_times_ok |= c != '+';
  637. X          many_times_ok |= c != '?';
  638. X          if (p == pend)
  639. X        break;
  640. X          PATFETCH (c);
  641. X          if (c == '*')
  642. X        ;
  643. X          else if (!(obscure_syntax & RE_BK_PLUS_QM)
  644. X               && (c == '+' || c == '?'))
  645. X        ;
  646. X          else if ((obscure_syntax & RE_BK_PLUS_QM)
  647. X               && c == '\\')
  648. X        {
  649. X          int c1;
  650. X          PATFETCH (c1);
  651. X          if (!(c1 == '+' || c1 == '?'))
  652. X            {
  653. X              PATUNFETCH;
  654. X              PATUNFETCH;
  655. X              break;
  656. X            }
  657. X          c = c1;
  658. X        }
  659. X          else
  660. X        {
  661. X          PATUNFETCH;
  662. X          break;
  663. X        }
  664. X        }
  665. X
  666. X      /* Star, etc. applied to an empty pattern is equivalent
  667. X         to an empty pattern.  */
  668. X      if (!laststart)
  669. X        break;
  670. X
  671. X      /* Now we know whether 0 matches is allowed,
  672. X         and whether 2 or more matches is allowed.  */
  673. X      if (many_times_ok)
  674. X        {
  675. X          /* If more than one repetition is allowed,
  676. X         put in a backward jump at the end.  */
  677. X          store_jump (b, maybe_finalize_jump, laststart - 3);
  678. X          b += 3;
  679. X        }
  680. X      insert_jump (on_failure_jump, laststart, b + 3, b);
  681. X      pending_exact = 0;
  682. X      b += 3;
  683. X      if (!zero_times_ok)
  684. X        {
  685. X          /* At least one repetition required: insert before the loop
  686. X         a skip over the initial on-failure-jump instruction */
  687. X          insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  688. X          b += 3;
  689. X        }
  690. X      break;
  691. X
  692. X    case '.':
  693. X      laststart = b;
  694. X      PATPUSH (anychar);
  695. X      break;
  696. X
  697. X    case '[':
  698. X      while (b - bufp->buffer
  699. X         > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  700. X        /* Note that EXTEND_BUFFER clobbers c */
  701. X        EXTEND_BUFFER;
  702. X
  703. X      laststart = b;
  704. X      if (*p == '^')
  705. X        PATPUSH (charset_not), p++;
  706. X      else
  707. X        PATPUSH (charset);
  708. X      p1 = p;
  709. X
  710. X      PATPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  711. X      /* Clear the whole map */
  712. X      bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  713. X      /* Read in characters and ranges, setting map bits */
  714. X      while (1)
  715. X        {
  716. X          PATFETCH (c);
  717. X
  718. X          /* If awk, \ escapes characters inside [...].  */
  719. X          if ((obscure_syntax & RE_AWK_CLASS_HACK) && c == '\\')
  720. X            {
  721. X              PATFETCH(c1);
  722. X              b[c1 / BYTEWIDTH] |= 1 << (c1 % BYTEWIDTH);
  723. X              continue;
  724. X            }
  725. X
  726. X          if (c == ']' && p != p1 + 1) break;
  727. X          if (*p == '-' && p[1] != ']')
  728. X        {
  729. X          PATFETCH (c1);
  730. X          PATFETCH (c1);
  731. X          while (c <= c1)
  732. X            b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH), c++;
  733. X        }
  734. X          else
  735. X        {
  736. X          b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH);
  737. X        }
  738. X        }
  739. X      /* Discard any bitmap bytes that are all 0 at the end of the map.
  740. X         Decrement the map-length byte too. */
  741. X      while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
  742. X        b[-1]--;
  743. X      b += b[-1];
  744. X      break;
  745. X
  746. X    case '(':
  747. X      if (! (obscure_syntax & RE_NO_BK_PARENS))
  748. X        goto normal_char;
  749. X      else
  750. X        goto handle_open;
  751. X
  752. X    case ')':
  753. X      if (! (obscure_syntax & RE_NO_BK_PARENS))
  754. X        goto normal_char;
  755. X      else
  756. X        goto handle_close;
  757. X
  758. X    case '\n':
  759. X      if (! (obscure_syntax & RE_NEWLINE_OR))
  760. X        goto normal_char;
  761. X      else
  762. X        goto handle_bar;
  763. X
  764. X    case '|':
  765. X      if (! (obscure_syntax & RE_NO_BK_VBAR))
  766. X        goto normal_char;
  767. X      else
  768. X        goto handle_bar;
  769. X
  770. X        case '\\':
  771. X      if (p == pend) goto invalid_pattern;
  772. X      PATFETCH_RAW (c);
  773. X      switch (c)
  774. X        {
  775. X        case '(':
  776. X          if (obscure_syntax & RE_NO_BK_PARENS)
  777. X        goto normal_backsl;
  778. X        handle_open:
  779. X          if (stackp == stacke) goto nesting_too_deep;
  780. X          if (regnum < RE_NREGS)
  781. X            {
  782. X          PATPUSH (start_memory);
  783. X          PATPUSH (regnum);
  784. X            }
  785. X          *stackp++ = b - bufp->buffer;
  786. X          *stackp++ = fixup_jump ? fixup_jump - bufp->buffer + 1 : 0;
  787. X          *stackp++ = regnum++;
  788. X          *stackp++ = begalt - bufp->buffer;
  789. X          fixup_jump = 0;
  790. X          laststart = 0;
  791. X          begalt = b;
  792. X          break;
  793. X
  794. X        case ')':
  795. X          if (obscure_syntax & RE_NO_BK_PARENS)
  796. X        goto normal_backsl;
  797. X        handle_close:
  798. X          if (stackp == stackb) goto unmatched_close;
  799. X          begalt = *--stackp + bufp->buffer;
  800. X          if (fixup_jump)
  801. X        store_jump (fixup_jump, jump, b);
  802. X          if (stackp[-1] < RE_NREGS)
  803. X        {
  804. X          PATPUSH (stop_memory);
  805. X          PATPUSH (stackp[-1]);
  806. X        }
  807. X          stackp -= 2;
  808. X          fixup_jump = 0;
  809. X          if (*stackp)
  810. X        fixup_jump = *stackp + bufp->buffer - 1;
  811. X          laststart = *--stackp + bufp->buffer;
  812. X          break;
  813. X
  814. X        case '|':
  815. X          if (obscure_syntax & RE_NO_BK_VBAR)
  816. X        goto normal_backsl;
  817. X        handle_bar:
  818. X          insert_jump (on_failure_jump, begalt, b + 6, b);
  819. X          pending_exact = 0;
  820. X          b += 3;
  821. X          if (fixup_jump)
  822. X        store_jump (fixup_jump, jump, b);
  823. X          fixup_jump = b;
  824. X          b += 3;
  825. X          laststart = 0;
  826. X          begalt = b;
  827. X          break;
  828. X
  829. X#ifdef emacs
  830. X        case '=':
  831. X          PATPUSH (at_dot);
  832. X          break;
  833. X
  834. X        case 's':    
  835. X          laststart = b;
  836. X          PATPUSH (syntaxspec);
  837. X          PATFETCH (c);
  838. X          PATPUSH (syntax_spec_code[c]);
  839. X          break;
  840. X
  841. X        case 'S':
  842. X          laststart = b;
  843. X          PATPUSH (notsyntaxspec);
  844. X          PATFETCH (c);
  845. X          PATPUSH (syntax_spec_code[c]);
  846. X          break;
  847. X#endif /* emacs */
  848. X
  849. X        case 'w':
  850. X          laststart = b;
  851. X          PATPUSH (wordchar);
  852. X          break;
  853. X
  854. X        case 'W':
  855. X          laststart = b;
  856. X          PATPUSH (notwordchar);
  857. X          break;
  858. X
  859. X        case '<':
  860. X          PATPUSH (wordbeg);
  861. X          break;
  862. X
  863. X        case '>':
  864. X          PATPUSH (wordend);
  865. X          break;
  866. X
  867. X        case 'b':
  868. X          PATPUSH (wordbound);
  869. X          break;
  870. X
  871. X        case 'B':
  872. X          PATPUSH (notwordbound);
  873. X          break;
  874. X
  875. X        case '`':
  876. X          PATPUSH (begbuf);
  877. X          break;
  878. X
  879. X        case '\'':
  880. X          PATPUSH (endbuf);
  881. X          break;
  882. X
  883. X        case '1':
  884. X        case '2':
  885. X        case '3':
  886. X        case '4':
  887. X        case '5':
  888. X        case '6':
  889. X        case '7':
  890. X        case '8':
  891. X        case '9':
  892. X          c1 = c - '0';
  893. X          if (c1 >= regnum)
  894. X        goto normal_char;
  895. X          for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  896. X         if (*stackt == c1)
  897. X          goto normal_char;
  898. X          laststart = b;
  899. X          PATPUSH (duplicate);
  900. X          PATPUSH (c1);
  901. X          break;
  902. X
  903. X        case '+':
  904. X        case '?':
  905. X          if (obscure_syntax & RE_BK_PLUS_QM)
  906. X        goto handle_plus;
  907. X
  908. X        default:
  909. X        normal_backsl:
  910. X          /* You might think it would be useful for \ to mean
  911. X         not to translate; but if we don't translate it
  912. X         it will never match anything.  */
  913. X          if (translate) c = translate[c];
  914. X          goto normal_char;
  915. X        }
  916. X      break;
  917. X
  918. X    default:
  919. X    normal_char:
  920. X      if (!pending_exact || pending_exact + *pending_exact + 1 != b
  921. X          || *pending_exact == 0177 || *p == '*' || *p == '^'
  922. X          || ((obscure_syntax & RE_BK_PLUS_QM)
  923. X          ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  924. X          : (*p == '+' || *p == '?')))
  925. X        {
  926. X          laststart = b;
  927. X          PATPUSH (exactn);
  928. X          pending_exact = b;
  929. X          PATPUSH (0);
  930. X        }
  931. X      PATPUSH (c);
  932. X      (*pending_exact)++;
  933. X    }
  934. X    }
  935. X
  936. X  if (fixup_jump)
  937. X    store_jump (fixup_jump, jump, b);
  938. X
  939. X  if (stackp != stackb) goto unmatched_open;
  940. X
  941. X  bufp->used = b - bufp->buffer;
  942. X  return 0;
  943. X
  944. X invalid_pattern:
  945. X  return "Invalid regular expression";
  946. X
  947. X unmatched_open:
  948. X  return "Unmatched \\(";
  949. X
  950. X unmatched_close:
  951. X  return "Unmatched \\)";
  952. X
  953. X end_of_pattern:
  954. X  return "Premature end of regular expression";
  955. X
  956. X nesting_too_deep:
  957. X  return "Nesting too deep";
  958. X
  959. X too_big:
  960. X  return "Regular expression too big";
  961. X
  962. X memory_exhausted:
  963. X  return "Memory exhausted";
  964. X}
  965. X
  966. X/* Store where `from' points a jump operation to jump to where `to' points.
  967. X  `opcode' is the opcode to store. */
  968. X
  969. Xstatic int
  970. Xstore_jump (from, opcode, to)
  971. X     char *from, *to;
  972. X     char opcode;
  973. X{
  974. X  from[0] = opcode;
  975. X  from[1] = (to - (from + 3)) & 0377;
  976. X  from[2] = (to - (from + 3)) >> 8;
  977. X}
  978. X
  979. X/* Open up space at char FROM, and insert there a jump to TO.
  980. X   CURRENT_END gives te end of the storage no in use,
  981. X   so we know how much data to copy up.
  982. X   OP is the opcode of the jump to insert.
  983. X
  984. X   If you call this function, you must zero out pending_exact.  */
  985. X
  986. Xstatic int
  987. Xinsert_jump (op, from, to, current_end)
  988. X     char op;
  989. X     char *from, *to, *current_end;
  990. X{
  991. X  register char *pto = current_end + 3;
  992. X  register char *pfrom = current_end;
  993. X  while (pfrom != from)
  994. X    *--pto = *--pfrom;
  995. X  store_jump (from, op, to);
  996. X}
  997. X
  998. X/* Given a pattern, compute a fastmap from it.
  999. X The fastmap records which of the (1 << BYTEWIDTH) possible characters
  1000. X can start a string that matches the pattern.
  1001. X This fastmap is used by re_search to skip quickly over totally implausible text.
  1002. X
  1003. X The caller must supply the address of a (1 << BYTEWIDTH)-byte data area
  1004. X as bufp->fastmap.
  1005. X The other components of bufp describe the pattern to be used.  */
  1006. X
  1007. Xvoid
  1008. Xre_compile_fastmap (bufp)
  1009. X     struct re_pattern_buffer *bufp;
  1010. X{
  1011. X  unsigned char *pattern = (unsigned char *) bufp->buffer;
  1012. X  int size = bufp->used;
  1013. X  register char *fastmap = bufp->fastmap;
  1014. X  register unsigned char *p = pattern;
  1015. X  register unsigned char *pend = pattern + size;
  1016. X  register int j, k;
  1017. X  unsigned char *translate = (unsigned char *) bufp->translate;
  1018. X
  1019. X  unsigned char *stackb[NFAILURES];
  1020. X  unsigned char **stackp = stackb;
  1021. X
  1022. X  bzero (fastmap, (1 << BYTEWIDTH));
  1023. X  bufp->fastmap_accurate = 1;
  1024. X  bufp->can_be_null = 0;
  1025. X      
  1026. X  while (p)
  1027. X    {
  1028. X      if (p == pend)
  1029. X    {
  1030. X      bufp->can_be_null = 1;
  1031. X      break;
  1032. X    }
  1033. X#ifdef SWITCH_ENUM_BUG
  1034. X      switch ((int) ((enum regexpcode) *p++))
  1035. X#else
  1036. X      switch ((enum regexpcode) *p++)
  1037. X#endif
  1038. X    {
  1039. X    case exactn:
  1040. X      if (translate)
  1041. X        fastmap[translate[p[1]]] = 1;
  1042. X      else
  1043. X        fastmap[p[1]] = 1;
  1044. X      break;
  1045. X
  1046. X        case begline:
  1047. X        case before_dot:
  1048. X    case at_dot:
  1049. X    case after_dot:
  1050. X    case begbuf:
  1051. X    case endbuf:
  1052. X    case wordbound:
  1053. X    case notwordbound:
  1054. X    case wordbeg:
  1055. X    case wordend:
  1056. X      continue;
  1057. X
  1058. X    case endline:
  1059. X      if (translate)
  1060. X        fastmap[translate['\n']] = 1;
  1061. X      else
  1062. X        fastmap['\n'] = 1;
  1063. X      if (bufp->can_be_null != 1)
  1064. X        bufp->can_be_null = 2;
  1065. X      break;
  1066. X
  1067. X    case finalize_jump:
  1068. X    case maybe_finalize_jump:
  1069. X    case jump:
  1070. X    case dummy_failure_jump:
  1071. X      bufp->can_be_null = 1;
  1072. X      j = *p++ & 0377;
  1073. X      j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1074. X      p += j + 1;        /* The 1 compensates for missing ++ above */
  1075. X      if (j > 0)
  1076. X        continue;
  1077. X      /* Jump backward reached implies we just went through
  1078. X         the body of a loop and matched nothing.
  1079. X         Opcode jumped to should be an on_failure_jump.
  1080. X         Just treat it like an ordinary jump.
  1081. X         For a * loop, it has pushed its failure point already;
  1082. X         if so, discard that as redundant.  */
  1083. X      if ((enum regexpcode) *p != on_failure_jump)
  1084. X        continue;
  1085. X      p++;
  1086. X      j = *p++ & 0377;
  1087. X      j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1088. X      p += j + 1;        /* The 1 compensates for missing ++ above */
  1089. X      if (stackp != stackb && *stackp == p)
  1090. X        stackp--;
  1091. X      continue;
  1092. X      
  1093. X    case on_failure_jump:
  1094. X      j = *p++ & 0377;
  1095. X      j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1096. X      p++;
  1097. X      *++stackp = p + j;
  1098. X      continue;
  1099. X
  1100. X    case start_memory:
  1101. X    case stop_memory:
  1102. X      p++;
  1103. X      continue;
  1104. X
  1105. X    case duplicate:
  1106. X      bufp->can_be_null = 1;
  1107. X      fastmap['\n'] = 1;
  1108. X    case anychar:
  1109. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  1110. X        if (j != '\n')
  1111. X          fastmap[j] = 1;
  1112. X      if (bufp->can_be_null)
  1113. X        return;
  1114. X      /* Don't return; check the alternative paths
  1115. X         so we can set can_be_null if appropriate.  */
  1116. X      break;
  1117. X
  1118. X    case wordchar:
  1119. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  1120. X        if (SYNTAX (j) == Sword)
  1121. X          fastmap[j] = 1;
  1122. X      break;
  1123. X
  1124. X    case notwordchar:
  1125. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  1126. X        if (SYNTAX (j) != Sword)
  1127. X          fastmap[j] = 1;
  1128. X      break;
  1129. X
  1130. X#ifdef emacs
  1131. X    case syntaxspec:
  1132. X      k = *p++;
  1133. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  1134. X        if (SYNTAX (j) == (enum syntaxcode) k)
  1135. X          fastmap[j] = 1;
  1136. X      break;
  1137. X
  1138. X    case notsyntaxspec:
  1139. X      k = *p++;
  1140. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  1141. X        if (SYNTAX (j) != (enum syntaxcode) k)
  1142. X          fastmap[j] = 1;
  1143. X      break;
  1144. X#endif /* emacs */
  1145. X
  1146. X    case charset:
  1147. X      for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1148. X        if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  1149. X          {
  1150. X        if (translate)
  1151. X          fastmap[translate[j]] = 1;
  1152. X        else
  1153. X          fastmap[j] = 1;
  1154. X          }
  1155. X      break;
  1156. X
  1157. X    case charset_not:
  1158. X      /* Chars beyond end of map must be allowed */
  1159. X      for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  1160. X        if (translate)
  1161. X          fastmap[translate[j]] = 1;
  1162. X        else
  1163. X          fastmap[j] = 1;
  1164. X
  1165. X      for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1166. X        if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  1167. X          {
  1168. X        if (translate)
  1169. X          fastmap[translate[j]] = 1;
  1170. X        else
  1171. X          fastmap[j] = 1;
  1172. X          }
  1173. X      break;
  1174. X    }
  1175. X
  1176. X      /* Get here means we have successfully found the possible starting characters
  1177. X     of one path of the pattern.  We need not follow this path any farther.
  1178. X     Instead, look at the next alternative remembered in the stack. */
  1179. X      if (stackp != stackb)
  1180. X    p = *stackp--;
  1181. X      else
  1182. X    break;
  1183. X    }
  1184. X}
  1185. X
  1186. X/* Like re_search_2, below, but only one string is specified. */
  1187. X
  1188. Xint
  1189. Xre_search (pbufp, string, size, startpos, range, regs)
  1190. X     struct re_pattern_buffer *pbufp;
  1191. X     char *string;
  1192. X     int size, startpos, range;
  1193. X     struct re_registers *regs;
  1194. X{
  1195. X  return re_search_2 (pbufp, 0, 0, string, size, startpos, range, regs, size);
  1196. X}
  1197. X
  1198. X/* Like re_match_2 but tries first a match starting at index STARTPOS,
  1199. X   then at STARTPOS + 1, and so on.
  1200. X   RANGE is the number of places to try before giving up.
  1201. X   If RANGE is negative, the starting positions tried are
  1202. X    STARTPOS, STARTPOS - 1, etc.
  1203. X   It is up to the caller to make sure that range is not so large
  1204. X   as to take the starting position outside of the input strings.
  1205. X
  1206. XThe value returned is the position at which the match was found,
  1207. X or -1 if no match was found,
  1208. X or -2 if error (such as failure stack overflow).  */
  1209. X
  1210. Xint
  1211. Xre_search_2 (pbufp, string1, size1, string2, size2, startpos, range, regs, mstop)
  1212. X     struct re_pattern_buffer *pbufp;
  1213. X     char *string1, *string2;
  1214. X     int size1, size2;
  1215. X     int startpos;
  1216. X     register int range;
  1217. X     struct re_registers *regs;
  1218. X     int mstop;
  1219. X{
  1220. X  register char *fastmap = pbufp->fastmap;
  1221. X  register unsigned char *translate = (unsigned char *) pbufp->translate;
  1222. X  int total = size1 + size2;
  1223. X  int val;
  1224. X
  1225. X  /* Update the fastmap now if not correct already */
  1226. X  if (fastmap && !pbufp->fastmap_accurate)
  1227. X    re_compile_fastmap (pbufp);
  1228. X  
  1229. X  /* Don't waste time in a long search for a pattern
  1230. X     that says it is anchored.  */
  1231. X  if (pbufp->used > 0 && (enum regexpcode) pbufp->buffer[0] == begbuf
  1232. X      && range > 0)
  1233. X    {
  1234. X      if (startpos > 0)
  1235. X    return -1;
  1236. X      else
  1237. X    range = 1;
  1238. X    }
  1239. X
  1240. X  while (1)
  1241. X    {
  1242. X      /* If a fastmap is supplied, skip quickly over characters
  1243. X     that cannot possibly be the start of a match.
  1244. X     Note, however, that if the pattern can possibly match
  1245. X     the null string, we must test it at each starting point
  1246. X     so that we take the first null string we get.  */
  1247. X
  1248. X      if (fastmap && startpos < total && pbufp->can_be_null != 1)
  1249. X    {
  1250. X      if (range > 0)
  1251. X        {
  1252. X          register int lim = 0;
  1253. X          register unsigned char *p;
  1254. X          int irange = range;
  1255. X          if (startpos < size1 && startpos + range >= size1)
  1256. X        lim = range - (size1 - startpos);
  1257. X
  1258. X          p = ((unsigned char *)
  1259. X           &(startpos >= size1 ? string2 - size1 : string1)[startpos]);
  1260. X
  1261. X          if (translate)
  1262. X        {
  1263. X          while (range > lim && !fastmap[translate[*p++]])
  1264. X            range--;
  1265. X        }
  1266. X          else
  1267. X        {
  1268. X          while (range > lim && !fastmap[*p++])
  1269. X            range--;
  1270. X        }
  1271. X          startpos += irange - range;
  1272. X        }
  1273. X      else
  1274. X        {
  1275. X          register unsigned char c;
  1276. X          if (startpos >= size1)
  1277. X        c = string2[startpos - size1];
  1278. X          else
  1279. X        c = string1[startpos];
  1280. X          c &= 0xff;
  1281. X          if (translate ? !fastmap[translate[c]] : !fastmap[c])
  1282. X        goto advance;
  1283. X        }
  1284. X    }
  1285. X
  1286. X      if (range >= 0 && startpos == total
  1287. X      && fastmap && pbufp->can_be_null == 0)
  1288. X    return -1;
  1289. X
  1290. X      val = re_match_2 (pbufp, string1, size1, string2, size2, startpos, regs, mstop);
  1291. X      if (0 <= val)
  1292. X    {
  1293. X      if (val == -2)
  1294. X        return -2;
  1295. X      return startpos;
  1296. X    }
  1297. X
  1298. X#ifdef C_ALLOCA
  1299. X      alloca (0);
  1300. X#endif /* C_ALLOCA */
  1301. X
  1302. X    advance:
  1303. X      if (!range) break;
  1304. X      if (range > 0) range--, startpos++; else range++, startpos--;
  1305. X    }
  1306. X  return -1;
  1307. X}
  1308. X
  1309. X#ifndef emacs   /* emacs never uses this */
  1310. Xint
  1311. Xre_match (pbufp, string, size, pos, regs)
  1312. X     struct re_pattern_buffer *pbufp;
  1313. X     char *string;
  1314. X     int size, pos;
  1315. X     struct re_registers *regs;
  1316. X{
  1317. X  return re_match_2 (pbufp, 0, 0, string, size, pos, regs, size);
  1318. X}
  1319. X#endif /* emacs */
  1320. X
  1321. X/* Maximum size of failure stack.  Beyond this, overflow is an error.  */
  1322. X
  1323. Xint re_max_failures = 2000;
  1324. X
  1325. Xstatic int bcmp_translate();
  1326. X/* Match the pattern described by PBUFP
  1327. X   against data which is the virtual concatenation of STRING1 and STRING2.
  1328. X   SIZE1 and SIZE2 are the sizes of the two data strings.
  1329. X   Start the match at position POS.
  1330. X   Do not consider matching past the position MSTOP.
  1331. X
  1332. X   If pbufp->fastmap is nonzero, then it had better be up to date.
  1333. X
  1334. X   The reason that the data to match are specified as two components
  1335. X   which are to be regarded as concatenated
  1336. X   is so this function can be used directly on the contents of an Emacs buffer.
  1337. X
  1338. X   -1 is returned if there is no match.  -2 is returned if there is
  1339. X   an error (such as match stack overflow).  Otherwise the value is the length
  1340. X   of the substring which was matched.  */
  1341. X
  1342. Xint
  1343. Xre_match_2 (pbufp, string1, size1, string2, size2, pos, regs, mstop)
  1344. X     struct re_pattern_buffer *pbufp;
  1345. X     unsigned char *string1, *string2;
  1346. X     int size1, size2;
  1347. X     int pos;
  1348. X     struct re_registers *regs;
  1349. X     int mstop;
  1350. X{
  1351. X  register unsigned char *p = (unsigned char *) pbufp->buffer;
  1352. X  register unsigned char *pend = p + pbufp->used;
  1353. X  /* End of first string */
  1354. X  unsigned char *end1;
  1355. X  /* End of second string */
  1356. X  unsigned char *end2;
  1357. X  /* Pointer just past last char to consider matching */
  1358. X  unsigned char *end_match_1, *end_match_2;
  1359. X  register unsigned char *d, *dend;
  1360. X  register int mcnt;
  1361. X  unsigned char *translate = (unsigned char *) pbufp->translate;
  1362. X
  1363. X /* Failure point stack.  Each place that can handle a failure further down the line
  1364. X    pushes a failure point on this stack.  It consists of two char *'s.
  1365. X    The first one pushed is where to resume scanning the pattern;
  1366. X    the second pushed is where to resume scanning the strings.
  1367. X    If the latter is zero, the failure point is a "dummy".
  1368. X    If a failure happens and the innermost failure point is dormant,
  1369. X    it discards that failure point and tries the next one. */
  1370. X
  1371. X  unsigned char *initial_stack[2 * NFAILURES];
  1372. X  unsigned char **stackb = initial_stack;
  1373. X  unsigned char **stackp = stackb, **stacke = &stackb[2 * NFAILURES];
  1374. X
  1375. X  /* Information on the "contents" of registers.
  1376. X     These are pointers into the input strings; they record
  1377. X     just what was matched (on this attempt) by some part of the pattern.
  1378. X     The start_memory command stores the start of a register's contents
  1379. X     and the stop_memory command stores the end.
  1380. X
  1381. X     At that point, regstart[regnum] points to the first character in the register,
  1382. X     regend[regnum] points to the first character beyond the end of the register,
  1383. X     regstart_seg1[regnum] is true iff regstart[regnum] points into string1,
  1384. X     and regend_seg1[regnum] is true iff regend[regnum] points into string1.  */
  1385. X
  1386. X  unsigned char *regstart[RE_NREGS];
  1387. X  unsigned char *regend[RE_NREGS];
  1388. X  unsigned char regstart_seg1[RE_NREGS], regend_seg1[RE_NREGS];
  1389. X
  1390. X  /* Set up pointers to ends of strings.
  1391. X     Don't allow the second string to be empty unless both are empty.  */
  1392. X  if (!size2)
  1393. X    {
  1394. X      string2 = string1;
  1395. X      size2 = size1;
  1396. X      string1 = 0;
  1397. X      size1 = 0;
  1398. X    }
  1399. X  end1 = string1 + size1;
  1400. X  end2 = string2 + size2;
  1401. X
  1402. X  /* Compute where to stop matching, within the two strings */
  1403. X  if (mstop <= size1)
  1404. X    {
  1405. X      end_match_1 = string1 + mstop;
  1406. X      end_match_2 = string2;
  1407. X    }
  1408. X  else
  1409. X    {
  1410. X      end_match_1 = end1;
  1411. X      end_match_2 = string2 + mstop - size1;
  1412. X    }
  1413. X
  1414. X  /* Initialize \) text positions to -1
  1415. X     to mark ones that no \( or \) has been seen for.  */
  1416. X
  1417. X  for (mcnt = 0; mcnt < sizeof (regend) / sizeof (*regend); mcnt++)
  1418. X    regend[mcnt] = (unsigned char *) -1;
  1419. X
  1420. X  /* `p' scans through the pattern as `d' scans through the data.
  1421. X     `dend' is the end of the input string that `d' points within.
  1422. X     `d' is advanced into the following input string whenever necessary,
  1423. X     but this happens before fetching;
  1424. X     therefore, at the beginning of the loop,
  1425. X     `d' can be pointing at the end of a string,
  1426. X     but it cannot equal string2.  */
  1427. X
  1428. X  if (pos <= size1)
  1429. X    d = string1 + pos, dend = end_match_1;
  1430. X  else
  1431. X    d = string2 + pos - size1, dend = end_match_2;
  1432. X
  1433. X/* Write PREFETCH; just before fetching a character with *d.  */
  1434. X#define PREFETCH \
  1435. X while (d == dend)                            \
  1436. X  { if (dend == end_match_2) goto fail;  /* end of string2 => failure */   \
  1437. X    d = string2;  /* end of string1 => advance to string2. */       \
  1438. X    dend = end_match_2; }
  1439. X
  1440. X  /* This loop loops over pattern commands.
  1441. X     It exits by returning from the function if match is complete,
  1442. X     or it drops through if match fails at this starting point in the input data. */
  1443. X
  1444. X  while (1)
  1445. X    {
  1446. X      if (p == pend)
  1447. X    /* End of pattern means we have succeeded! */
  1448. X    {
  1449. X      /* If caller wants register contents data back, convert it to indices */
  1450. X      if (regs)
  1451. X        {
  1452. X           regs->start[0] = pos;
  1453. X           if (dend == end_match_1)
  1454. X         regs->end[0] = d - string1;
  1455. X           else
  1456. X         regs->end[0] = d - string2 + size1;
  1457. X           for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  1458. X        {
  1459. X          if (regend[mcnt] == (unsigned char *) -1)
  1460. X            {
  1461. X              regs->start[mcnt] = -1;
  1462. X              regs->end[mcnt] = -1;
  1463. X              continue;
  1464. X            }
  1465. X           if (regstart_seg1[mcnt])
  1466. X            regs->start[mcnt] = regstart[mcnt] - string1;
  1467. X          else
  1468. X            regs->start[mcnt] = regstart[mcnt] - string2 + size1;
  1469. X           if (regend_seg1[mcnt])
  1470. X            regs->end[mcnt] = regend[mcnt] - string1;
  1471. X          else
  1472. X            regs->end[mcnt] = regend[mcnt] - string2 + size1;
  1473. X        }
  1474. X        }
  1475. X       if (dend == end_match_1)
  1476. X        return (d - string1 - pos);
  1477. X      else
  1478. X        return d - string2 + size1 - pos;
  1479. X    }
  1480. X
  1481. X      /* Otherwise match next pattern command */
  1482. X#ifdef SWITCH_ENUM_BUG
  1483. X      switch ((int) ((enum regexpcode) *p++))
  1484. X#else
  1485. X      switch ((enum regexpcode) *p++)
  1486. X#endif
  1487. X    {
  1488. X
  1489. X    /* \( is represented by a start_memory, \) by a stop_memory.
  1490. X        Both of those commands contain a "register number" argument.
  1491. X        The text matched within the \( and \) is recorded under that number.
  1492. X        Then, \<digit> turns into a `duplicate' command which
  1493. X        is followed by the numeric value of <digit> as the register number. */
  1494. X
  1495. X    case start_memory:
  1496. X      regstart[*p] = d;
  1497. X       regstart_seg1[*p++] = (dend == end_match_1);
  1498. X      break;
  1499. X
  1500. X    case stop_memory:
  1501. X      regend[*p] = d;
  1502. X       regend_seg1[*p++] = (dend == end_match_1);
  1503. X      break;
  1504. X
  1505. X    case duplicate:
  1506. X      {
  1507. X        int regno = *p++;   /* Get which register to match against */
  1508. X        register unsigned char *d2, *dend2;
  1509. X
  1510. X        d2 = regstart[regno];
  1511. X         dend2 = ((regstart_seg1[regno] == regend_seg1[regno])
  1512. X             ? regend[regno] : end_match_1);
  1513. X        while (1)
  1514. X          {
  1515. X        /* Advance to next segment in register contents, if necessary */
  1516. X        while (d2 == dend2)
  1517. X          {
  1518. X            if (dend2 == end_match_2) break;
  1519. X            if (dend2 == regend[regno]) break;
  1520. X            d2 = string2, dend2 = regend[regno];  /* end of string1 => advance to string2. */
  1521. X          }
  1522. X        /* At end of register contents => success */
  1523. X        if (d2 == dend2) break;
  1524. X
  1525. X        /* Advance to next segment in data being matched, if necessary */
  1526. X        PREFETCH;
  1527. X
  1528. X        /* mcnt gets # consecutive chars to compare */
  1529. X        mcnt = dend - d;
  1530. X        if (mcnt > dend2 - d2)
  1531. X          mcnt = dend2 - d2;
  1532. X        /* Compare that many; failure if mismatch, else skip them. */
  1533. X        if (translate ? bcmp_translate (d, d2, mcnt, translate) : bcmp (d, d2, mcnt))
  1534. X          goto fail;
  1535. X        d += mcnt, d2 += mcnt;
  1536. X          }
  1537. X      }
  1538. X      break;
  1539. X
  1540. X    case anychar:
  1541. X      /* fetch a data character */
  1542. X      PREFETCH;
  1543. X      /* Match anything but a newline.  */
  1544. X      if ((translate ? translate[*d++] : *d++) == '\n')
  1545. X        goto fail;
  1546. X      break;
  1547. X
  1548. X    case charset:
  1549. X    case charset_not:
  1550. X      {
  1551. X        /* Nonzero for charset_not */
  1552. X        int not = 0;
  1553. X        register int c;
  1554. X        if (*(p - 1) == (unsigned char) charset_not)
  1555. X          not = 1;
  1556. X
  1557. X        /* fetch a data character */
  1558. X        PREFETCH;
  1559. X
  1560. X        if (translate)
  1561. X          c = translate [*d];
  1562. X        else
  1563. X          c = *d;
  1564. X
  1565. X        if (c < *p * BYTEWIDTH
  1566. X        && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  1567. X          not = !not;
  1568. X
  1569. X        p += 1 + *p;
  1570. X
  1571. X        if (!not) goto fail;
  1572. X        d++;
  1573. X        break;
  1574. X      }
  1575. X
  1576. X    case begline:
  1577. X      if (d == string1 || d[-1] == '\n')
  1578. X        break;
  1579. X      goto fail;
  1580. X
  1581. X    case endline:
  1582. X      if (d == end2
  1583. X          || (d == end1 ? (size2 == 0 || *string2 == '\n') : *d == '\n'))
  1584. X        break;
  1585. X      goto fail;
  1586. X
  1587. X    /* "or" constructs ("|") are handled by starting each alternative
  1588. X        with an on_failure_jump that points to the start of the next alternative.
  1589. X        Each alternative except the last ends with a jump to the joining point.
  1590. X        (Actually, each jump except for the last one really jumps
  1591. X         to the following jump, because tensioning the jumps is a hassle.) */
  1592. X
  1593. X    /* The start of a stupid repeat has an on_failure_jump that points
  1594. X       past the end of the repeat text.
  1595. X       This makes a failure point so that, on failure to match a repetition,
  1596. X       matching restarts past as many repetitions have been found
  1597. X       with no way to fail and look for another one.  */
  1598. X
  1599. X    /* A smart repeat is similar but loops back to the on_failure_jump
  1600. X       so that each repetition makes another failure point. */
  1601. X
  1602. X    case on_failure_jump:
  1603. X      if (stackp == stacke)
  1604. X        {
  1605. X          unsigned char **stackx;
  1606. X          if (stacke - stackb > re_max_failures * 2)
  1607. X        return -2;
  1608. X          stackx = (unsigned char **) alloca (2 * (stacke - stackb)
  1609. X                     * sizeof (char *));
  1610. X          bcopy (stackb, stackx, (stacke - stackb) * sizeof (char *));
  1611. X          stackp = stackx + (stackp - stackb);
  1612. X          stacke = stackx + 2 * (stacke - stackb);
  1613. X          stackb = stackx;
  1614. X        }
  1615. X      mcnt = *p++ & 0377;
  1616. X      mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1617. X      p++;
  1618. X      *stackp++ = mcnt + p;
  1619. X      *stackp++ = d;
  1620. X      break;
  1621. X
  1622. X    /* The end of a smart repeat has an maybe_finalize_jump back.
  1623. X       Change it either to a finalize_jump or an ordinary jump. */
  1624. X
  1625. X    case maybe_finalize_jump:
  1626. X      mcnt = *p++ & 0377;
  1627. X      mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1628. X      p++;
  1629. X      {
  1630. X        register unsigned char *p2 = p;
  1631. X        /* Compare what follows with the begining of the repeat.
  1632. X           If we can establish that there is nothing that they would
  1633. X           both match, we can change to finalize_jump */
  1634. X        while (p2 != pend
  1635. X           && (*p2 == (unsigned char) stop_memory
  1636. X               || *p2 == (unsigned char) start_memory))
  1637. X          p2++;
  1638. X        if (p2 == pend)
  1639. X          p[-3] = (unsigned char) finalize_jump;
  1640. X        else if (*p2 == (unsigned char) exactn
  1641. X             || *p2 == (unsigned char) endline)
  1642. X          {
  1643. X        register int c = *p2 == (unsigned char) endline ? '\n' : p2[2];
  1644. X        register unsigned char *p1 = p + mcnt;
  1645. X        /* p1[0] ... p1[2] are an on_failure_jump.
  1646. X           Examine what follows that */
  1647. X        if (p1[3] == (unsigned char) exactn && p1[5] != c)
  1648. X          p[-3] = (unsigned char) finalize_jump;
  1649. X        else if (p1[3] == (unsigned char) charset
  1650. X             || p1[3] == (unsigned char) charset_not)
  1651. X          {
  1652. X            int not = p1[3] == (unsigned char) charset_not;
  1653. X            if (c < p1[4] * BYTEWIDTH
  1654. X            && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  1655. X              not = !not;
  1656. X            /* not is 1 if c would match */
  1657. X            /* That means it is not safe to finalize */
  1658. X            if (!not)
  1659. X              p[-3] = (unsigned char) finalize_jump;
  1660. X          }
  1661. X          }
  1662. X      }
  1663. X      p -= 2;
  1664. X      if (p[-1] != (unsigned char) finalize_jump)
  1665. X        {
  1666. X          p[-1] = (unsigned char) jump;
  1667. X          goto nofinalize;
  1668. X        }
  1669. X
  1670. X    /* The end of a stupid repeat has a finalize-jump
  1671. X       back to the start, where another failure point will be made
  1672. X       which will point after all the repetitions found so far. */
  1673. X
  1674. X    case finalize_jump:
  1675. X      stackp -= 2;
  1676. X
  1677. X    case jump:
  1678. X    nofinalize:
  1679. X      mcnt = *p++ & 0377;
  1680. X      mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1681. X      p += mcnt + 1;    /* The 1 compensates for missing ++ above */
  1682. X      break;
  1683. X
  1684. X    case dummy_failure_jump:
  1685. X      if (stackp == stacke)
  1686. X        {
  1687. X          unsigned char **stackx
  1688. X        = (unsigned char **) alloca (2 * (stacke - stackb)
  1689. X                         * sizeof (char *));
  1690. X          bcopy (stackb, stackx, (stacke - stackb) * sizeof (char *));
  1691. X          stackp = stackx + (stackp - stackb);
  1692. X          stacke = stackx + 2 * (stacke - stackb);
  1693. X          stackb = stackx;
  1694. X        }
  1695. X      *stackp++ = 0;
  1696. X      *stackp++ = 0;
  1697. X      goto nofinalize;
  1698. X
  1699. X    case wordbound:
  1700. X      if (d == string1  /* Points to first char */
  1701. X          || d == end2  /* Points to end */
  1702. X          || (d == end1 && size2 == 0)) /* Points to end */
  1703. X        break;
  1704. X      if ((SYNTAX (d[-1]) == Sword)
  1705. X          != (SYNTAX (d == end1 ? *string2 : *d) == Sword))
  1706. X        break;
  1707. X      goto fail;
  1708. X
  1709. X    case notwordbound:
  1710. X      if (d == string1  /* Points to first char */
  1711. X          || d == end2  /* Points to end */
  1712. X          || (d == end1 && size2 == 0)) /* Points to end */
  1713. X        goto fail;
  1714. X      if ((SYNTAX (d[-1]) == Sword)
  1715. X          != (SYNTAX (d == end1 ? *string2 : *d) == Sword))
  1716. X        goto fail;
  1717. X      break;
  1718. X
  1719. X    case wordbeg:
  1720. X      if (d == end2  /* Points to end */
  1721. X          || (d == end1 && size2 == 0) /* Points to end */
  1722. X          || SYNTAX (* (d == end1 ? string2 : d)) != Sword) /* Next char not a letter */
  1723. X        goto fail;
  1724. X      if (d == string1  /* Points to first char */
  1725. X          || SYNTAX (d[-1]) != Sword)  /* prev char not letter */
  1726. X        break;
  1727. X      goto fail;
  1728. X
  1729. X    case wordend:
  1730. X      if (d == string1  /* Points to first char */
  1731. X          || SYNTAX (d[-1]) != Sword)  /* prev char not letter */
  1732. X        goto fail;
  1733. X      if (d == end2  /* Points to end */
  1734. X          || (d == end1 && size2 == 0) /* Points to end */
  1735. X          || SYNTAX (d == end1 ? *string2 : *d) != Sword) /* Next char not a letter */
  1736. X        break;
  1737. X      goto fail;
  1738. X
  1739. X#ifdef emacs
  1740. X    case before_dot:
  1741. X      if (((d - string2 <= (unsigned) size2)
  1742. X           ? d - bf_p2 : d - bf_p1)
  1743. X          <= point)
  1744. X        goto fail;
  1745. X      break;
  1746. X
  1747. X    case at_dot:
  1748. X      if (((d - string2 <= (unsigned) size2)
  1749. X           ? d - bf_p2 : d - bf_p1)
  1750. X          == point)
  1751. X        goto fail;
  1752. X      break;
  1753. X
  1754. X    case after_dot:
  1755. X      if (((d - string2 <= (unsigned) size2)
  1756. X           ? d - bf_p2 : d - bf_p1)
  1757. X          >= point)
  1758. X        goto fail;
  1759. X      break;
  1760. X
  1761. X    case wordchar:
  1762. X      mcnt = (int) Sword;
  1763. X      goto matchsyntax;
  1764. X
  1765. X    case syntaxspec:
  1766. X      mcnt = *p++;
  1767. X    matchsyntax:
  1768. X      PREFETCH;
  1769. X      if (SYNTAX (*d++) != (enum syntaxcode) mcnt) goto fail;
  1770. X      break;
  1771. X      
  1772. X    case notwordchar:
  1773. X      mcnt = (int) Sword;
  1774. X      goto matchnotsyntax;
  1775. X
  1776. X    case notsyntaxspec:
  1777. X      mcnt = *p++;
  1778. X    matchnotsyntax:
  1779. X      PREFETCH;
  1780. X      if (SYNTAX (*d++) == (enum syntaxcode) mcnt) goto fail;
  1781. X      break;
  1782. X#else
  1783. X    case wordchar:
  1784. X      PREFETCH;
  1785. X      if (SYNTAX (*d++) == 0) goto fail;
  1786. X      break;
  1787. X      
  1788. X    case notwordchar:
  1789. X      PREFETCH;
  1790. X      if (SYNTAX (*d++) != 0) goto fail;
  1791. X      break;
  1792. X#endif /* not emacs */
  1793. X
  1794. X    case begbuf:
  1795. X      if (d == string1)    /* Note, d cannot equal string2 */
  1796. X        break;        /* unless string1 == string2.  */
  1797. X      goto fail;
  1798. X
  1799. X    case endbuf:
  1800. X      if (d == end2 || (d == end1 && size2 == 0))
  1801. X        break;
  1802. X      goto fail;
  1803. X
  1804. X    case exactn:
  1805. X      /* Match the next few pattern characters exactly.
  1806. X         mcnt is how many characters to match. */
  1807. X      mcnt = *p++;
  1808. X      if (translate)
  1809. X        {
  1810. X          do
  1811. X        {
  1812. X          PREFETCH;
  1813. X          if (translate[*d++] != *p++) goto fail;
  1814. X        }
  1815. X          while (--mcnt);
  1816. X        }
  1817. X      else
  1818. X        {
  1819. X          do
  1820. X        {
  1821. X          PREFETCH;
  1822. X          if (*d++ != *p++) goto fail;
  1823. X        }
  1824. X          while (--mcnt);
  1825. X        }
  1826. X      break;
  1827. X    }
  1828. X      continue;    /* Successfully matched one pattern command; keep matching */
  1829. X
  1830. X      /* Jump here if any matching operation fails. */
  1831. X    fail:
  1832. X      if (stackp != stackb)
  1833. X    /* A restart point is known.  Restart there and pop it. */
  1834. X    {
  1835. X      if (!stackp[-2])
  1836. X        {   /* If innermost failure point is dormant, flush it and keep looking */
  1837. X          stackp -= 2;
  1838. X          goto fail;
  1839. X        }
  1840. X      d = *--stackp;
  1841. X      p = *--stackp;
  1842. X      if (d >= string1 && d <= end1)
  1843. X        dend = end_match_1;
  1844. X    }
  1845. X      else break;   /* Matching at this starting point really fails! */
  1846. X    }
  1847. X  return -1;         /* Failure to match */
  1848. X}
  1849. X
  1850. Xstatic int
  1851. Xbcmp_translate (s1, s2, len, translate)
  1852. X     unsigned char *s1, *s2;
  1853. X     register int len;
  1854. X     unsigned char *translate;
  1855. X{
  1856. X  register unsigned char *p1 = s1, *p2 = s2;
  1857. X  while (len)
  1858. X    {
  1859. X      if (translate [*p1++] != translate [*p2++]) return 1;
  1860. X      len--;
  1861. X    }
  1862. X  return 0;
  1863. X}
  1864. X
  1865. X/* Entry points compatible with bsd4.2 regex library */
  1866. X
  1867. X#ifndef emacs
  1868. X
  1869. Xstatic struct re_pattern_buffer re_comp_buf;
  1870. X
  1871. Xchar *
  1872. Xre_comp (s)
  1873. X     char *s;
  1874. X{
  1875. X  if (!s)
  1876. X    {
  1877. X      if (!re_comp_buf.buffer)
  1878. X    return "No previous regular expression";
  1879. X      return 0;
  1880. X    }
  1881. X
  1882. X  if (!re_comp_buf.buffer)
  1883. X    {
  1884. X      if (!(re_comp_buf.buffer = (char *) malloc (200)))
  1885. X    return "Memory exhausted";
  1886. X      re_comp_buf.allocated = 200;
  1887. X      if (!(re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH)))
  1888. X    return "Memory exhausted";
  1889. X    }
  1890. X  return re_compile_pattern (s, strlen (s), &re_comp_buf);
  1891. X}
  1892. X
  1893. Xint
  1894. Xre_exec (s)
  1895. X     char *s;
  1896. X{
  1897. X  int len = strlen (s);
  1898. X  return 0 <= re_search (&re_comp_buf, s, len, 0, len, 0);
  1899. X}
  1900. X
  1901. X#endif /* emacs */
  1902. X
  1903. X#ifdef test
  1904. X
  1905. X#include <stdio.h>
  1906. X
  1907. X/* Indexed by a character, gives the upper case equivalent of the character */
  1908. X
  1909. Xstatic char upcase[0400] = 
  1910. X  { 000, 001, 002, 003, 004, 005, 006, 007,
  1911. X    010, 011, 012, 013, 014, 015, 016, 017,
  1912. X    020, 021, 022, 023, 024, 025, 026, 027,
  1913. X    030, 031, 032, 033, 034, 035, 036, 037,
  1914. X    040, 041, 042, 043, 044, 045, 046, 047,
  1915. X    050, 051, 052, 053, 054, 055, 056, 057,
  1916. X    060, 061, 062, 063, 064, 065, 066, 067,
  1917. X    070, 071, 072, 073, 074, 075, 076, 077,
  1918. X    0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  1919. X    0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  1920. X    0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  1921. X    0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
  1922. X    0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  1923. X    0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  1924. X    0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  1925. X    0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
  1926. X    0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  1927. X    0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  1928. X    0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
  1929. X    0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
  1930. X    0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
  1931. X    0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  1932. X    0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  1933. X    0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  1934. X    0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  1935. X    0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  1936. X    0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
  1937. X    0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  1938. X    0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  1939. X    0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
  1940. END_OF_FILE
  1941.   if test 49494 -ne `wc -c <'./regex.c.01'`; then
  1942.     echo shar: \"'./regex.c.01'\" unpacked with wrong size!
  1943.   fi
  1944.   # end of './regex.c.01'
  1945. fi
  1946. echo shar: End of archive 8 \(of 16\).
  1947. cp /dev/null ark8isdone
  1948. MISSING=""
  1949. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ; do
  1950.     if test ! -f ark${I}isdone ; then
  1951.     MISSING="${MISSING} ${I}"
  1952.     fi
  1953. done
  1954. if test "${MISSING}" = "" ; then
  1955.     echo You have unpacked all 16 archives.
  1956.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1957. else
  1958.     echo You still must unpack the following archives:
  1959.     echo "        " ${MISSING}
  1960. fi
  1961. exit 0
  1962. exit 0 # Just in case...
  1963.